package com.example.dell.fastshopping;

import android.os.Bundle;
import android.support.annotation.Dimension;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.dell.fastshopping.charge.ChargeFragment;
import com.example.dell.fastshopping.mall.DuoBaoMenuFragment;
import com.example.dell.fastshopping.home.MainFragment;
import com.example.dell.fastshopping.user.UserFragment;


import java.lang.reflect.Field;
import java.util.ArrayList;

import butterknife.BindView;


public class MainActivity extends AppCompatActivity {

    @BindView(R.id.container_VP)
    ViewPager mContainer_VP;
    @BindView(R.id.tab)
    TabLayout mTab;
    private String[] title = {"夺宝", "我的"};//"首页",  "钱包"
    private int[] drawables = {R.drawable.main_duobao_icon_select, R.drawable.main_wallet_icon_select};//R.drawable.main_home_icon_select,R.drawable.main_charge_icon_select,

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
        butterknife.ButterKnife.bind(this);
        init();
//        RefWatcher refWatcher = MyApplication.getRefWatcher(this);
//        refWatcher.watch(this);
    }

    protected void init() {
        TabLayout.Tab tt;
        final ArrayList<Fragment> fragments = new ArrayList<>();
        // fragments.add(MainFragment.newInstance());
        fragments.add(DuoBaoMenuFragment.newInstance());
        //  fragments.add(ChargeFragment.newInstance());
        fragments.add(UserFragment.newInstance());
        mTab.setupWithViewPager(mContainer_VP);
        mContainer_VP.setOffscreenPageLimit(2);
        mContainer_VP.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return fragments.get(i);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return title[position];
            }
        });
        mTab.removeAllTabs();
        for (int i = 0; i < title.length; i++) {
            tt = mTab.newTab();
            tt.setText(title[i]);
            tt.setIcon(getResources().getDrawable(drawables[i]));
            mTab.addTab(tt, i);
            Object o = tt.view;
            try {
                Field iconView = o.getClass().getDeclaredField("iconView");
                iconView.setAccessible(true);
                ImageView icon = (ImageView) iconView.get(o);
                LinearLayout.MarginLayoutParams lp = (LinearLayout.MarginLayoutParams) icon.getLayoutParams();
                int dp = dpToPx(0);
                lp.bottomMargin = dp;
                icon.setLayoutParams(lp);
                icon.requestLayout();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

    }

    public int dpToPx(@Dimension(unit = 0) int dps) {
        return Math.round(this.getResources().getDisplayMetrics().density * (float) dps);
    }
}
